package com.wealth.bank.web.controller;

import iaas.enums.StatusEnum;
import iaas.utils.ResultObject;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wealth.bank.entity.Article;
import com.wealth.bank.entity.ArticleCategory;
import com.wealth.bank.service.IArticleCategoryService;
import com.wealth.bank.service.IArticleService;
import com.wealth.page.entity.Ad;
import com.wealth.page.enums.AdTypeEnum;
import com.wealth.page.service.IADService;
import com.wealth.system.web.controller.BaseController;


/**
 * 银行文章
 * @author Administrator
 */


@Controller
@RequestMapping(value="/article")
public class ArticleController  extends BaseController{
	Logger logger = Logger.getLogger(ArticleController.class);
	
	@Autowired
	private IArticleCategoryService categoryService;
	@Autowired
	private IArticleService articleService;
	@Autowired
	private IADService adService;
	
	
	
	/**
	 * 首页展示银行文章和分类
	 * @param request
	 * @param memberid
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/index",method=RequestMethod.GET)
	public String index(HttpServletRequest request,Long memberid, Model model) {
		
		try {
			//查询文章分类
			List<ArticleCategory> articleCategories=categoryService.findByCorpIdAndStatus(corpid,StatusEnum.NORMAL.getValue());
			model.addAttribute("articleCategories", articleCategories);
			//查询文章
			List<Article> articles=articleService.getArticlePag(0,StatusEnum.NORMAL.getValue(),corpid);
			model.addAttribute("articles", articles);
			//查询银行广告
			List<Ad> ads=adService.getAdByCorpIdAndTypeAndStatus(corpid, AdTypeEnum.BANK.getValue(), StatusEnum.NORMAL.getValue());
			model.addAttribute("ads", ads);
			return "article/index";
		} catch (Exception e) {
			logger.error(e);
			return "system/500";
		}
	}

	

	/**
	 * 展示某分类下文章列表
	 * @param request
	 * @param memberid
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/articlelist/{categoryId}",method=RequestMethod.GET)
	public String articlelist(HttpServletRequest request, @PathVariable Long categoryId, Model model) {
		
		try {
			//查询某分类下的文章列表
			List<Article> articles=articleService.getArticlePagByCategoryId(0, StatusEnum.NORMAL.getValue(), categoryId, corpid);
			model.addAttribute("articles", articles);
			model.addAttribute("categoryId", categoryId);
			return "article/articlelist";
		} catch (Exception e) {
			logger.error("categoryId="+categoryId+""+e);
			return "system/500";
		}
	}
	
	/**
	 * 加载更多 某分类下文章列表
	 * @param request
	 * @param memberid
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/articlemove/{categoryId}")
	@ResponseBody
	public ResultObject articlemove(HttpServletRequest request, @PathVariable Long categoryId,Integer id, Model model) {
		
		try {
			//查询某分类下的文章列表
			List<Article> articles=articleService.getArticlePagByCategoryIdMove(id, StatusEnum.NORMAL.getValue(), categoryId, corpid);
			if(articles.size()>0){
				return new ResultObject("200", "加载成功", articles);
			}else{
				return new ResultObject("300", "已经最新", articles);
			}
		} catch (Exception e) {
			logger.error("categoryId="+categoryId+""+e);
			return null;
		}
	}
	
	
	
	/**
	 * 获取文章明细
	 * @param request
	 * @param articleId
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/articleinfo/{articleId}",method=RequestMethod.GET)
	public String articleinfo(HttpServletRequest request, @PathVariable Long articleId, Model model) {
		
		try {
			Article article=articleService.get(articleId);
			model.addAttribute("article", article);
			return "article/info";
		} catch (Exception e) {
			logger.error("articleId="+articleId+""+e);
			return "system/500";
		}
	}
	
	
	
	
	
	
	

}
